package com.oss.service.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oss.common.BaseResponse;
import com.oss.dto.dictionary.*;
import com.oss.entity.system.Dictionary;
import com.oss.enums.IsDeletedEnum;
import com.oss.mapper.system.DictionaryMapper;
import com.oss.util.SnowFlakeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author chengqiang
 */
@Service
public class DictionaryService extends ServiceImpl<DictionaryMapper, Dictionary> {
    @Autowired
    DictionaryMapper dictionaryMapper;

    /**
     * 新增字典值
     * 逻辑校验 :
     * 相同的字典类型code下, 不允许重复的字典值或字典名称
     * @param dto 入参
     * @return 返回提示信息
     */
    public BaseResponse<String> addDictionary(DictionaryAddDto dto) {
        LambdaQueryWrapper<Dictionary> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dictionary::getIsDeleted, IsDeletedEnum.NO.getFlag());
        lambdaQueryWrapper.eq(Dictionary::getDicTypeCode,dto.getDicTypeCode());
        lambdaQueryWrapper.and(
                wrapper->
                        wrapper.eq(Dictionary::getDicName,dto.getDicName())
                                .or()
                                .eq(Dictionary::getDicValue,dto.getDicValue())
                );
        long count = dictionaryMapper.selectCount(lambdaQueryWrapper);
        if (count > 0) {
            return BaseResponse.error("相同的字典类型code下, 不允许重复的字典值或字典名称");
        }
        Dictionary entity = new Dictionary();
        BeanUtils.copyProperties(dto,entity);
        entity.setDicId(SnowFlakeUtil.getID());
        dictionaryMapper.insert(entity);
        return BaseResponse.success("操作成功");
    }

    /**
     * 新增字典值
     * 逻辑校验 :
     * 相同的字典类型code下, 不允许重复的字典值或字典名称(与非当前记录比较)
     * @param dto 入参
     * @return 返回提示信息
     */
    public BaseResponse<String> updateDictionary(DictionaryUpdateDto dto) {
        LambdaQueryWrapper<Dictionary> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dictionary::getIsDeleted, IsDeletedEnum.NO.getFlag());
        // 非当前记录
        lambdaQueryWrapper.ne(Dictionary::getDicId, dto.getDicId());
        lambdaQueryWrapper.eq(Dictionary::getDicTypeCode,dto.getDicTypeCode());
        lambdaQueryWrapper.and(
                wrapper->
                        wrapper.eq(Dictionary::getDicName,dto.getDicName())
                                .or()
                                .eq(Dictionary::getDicValue,dto.getDicValue())
        );
        long count = dictionaryMapper.selectCount(lambdaQueryWrapper);
        if (count > 0) {
            return BaseResponse.error("相同的字典类型code下, 不允许重复的字典值或字典名称");
        }
        Dictionary entity = new Dictionary();
        BeanUtils.copyProperties(dto,entity);
        dictionaryMapper.updateById(entity);
        return BaseResponse.success("操作成功");

    }

    public String deleteDictionary(DictionaryDeleteDto dto) {
        LambdaUpdateWrapper<Dictionary> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Dictionary::getDicId,dto.getDicId());
        lambdaUpdateWrapper.set(Dictionary::getIsDeleted,IsDeletedEnum.YES.getFlag());
        dictionaryMapper.update(null,lambdaUpdateWrapper);
        return "操作成功";
    }

    public IPage<Dictionary> selectByPage(DictionaryPageDto dto) {
        LambdaQueryWrapper<Dictionary> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dictionary::getDicTypeCode,dto.getDicTypeCode());
        lambdaQueryWrapper.eq(Dictionary::getIsDeleted,IsDeletedEnum.NO.getFlag());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(dto.getDicName()), Dictionary::getDicName,dto.getDicName());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(dto.getDicValue()), Dictionary::getDicValue,dto.getDicValue());
        lambdaQueryWrapper.orderByAsc(Dictionary::getDicSort);
        return this.page(new Page(dto.getPageNo(),dto.getPageSize()),lambdaQueryWrapper);
    }

    public List<Dictionary> selectDictionaryByTypeCode(SelectDictionaryByTypeDto dto) {
        LambdaQueryWrapper<Dictionary> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dictionary::getIsDeleted,IsDeletedEnum.NO.getFlag());
        lambdaQueryWrapper.eq(Dictionary::getDicTypeCode,dto.getDicTypeCode());
        lambdaQueryWrapper.orderByAsc(Dictionary::getDicSort);
        return dictionaryMapper.selectList(lambdaQueryWrapper);
    }
    public List<Dictionary> selectDictionaryByTypeCode(String dicTypeCode) {
        LambdaQueryWrapper<Dictionary> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dictionary::getIsDeleted,IsDeletedEnum.NO.getFlag());
        lambdaQueryWrapper.eq(Dictionary::getDicTypeCode,dicTypeCode);
        lambdaQueryWrapper.orderByAsc(Dictionary::getDicSort);
        return dictionaryMapper.selectList(lambdaQueryWrapper);
    }
    public List<Dictionary> selectDictionaryByDicValue(Set<String> valueSet) {
        LambdaQueryWrapper<Dictionary> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dictionary::getIsDeleted,IsDeletedEnum.NO.getFlag());
        lambdaQueryWrapper.in(Dictionary::getDicValue,valueSet);
        lambdaQueryWrapper.orderByAsc(Dictionary::getDicSort);
        return dictionaryMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 根据字典类型code获取字典map key = 字典值  value = 字典名称 (数据库存的是字典值 , 回显时需要名称)
     * @param dicTypeCode
     * @return
     */
    public Map<String,String> getDictionaryMapByTypeCode(String dicTypeCode){
        LambdaQueryWrapper<Dictionary> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dictionary::getIsDeleted,IsDeletedEnum.NO.getFlag());
        lambdaQueryWrapper.eq(Dictionary::getDicTypeCode,dicTypeCode);
        lambdaQueryWrapper.orderByAsc(Dictionary::getDicSort);
        List<Dictionary> list =  dictionaryMapper.selectList(lambdaQueryWrapper);
        return list.stream().collect(Collectors.toMap(Dictionary::getDicValue,Dictionary::getDicName,(v1,v2) -> v2));
    }
    /**
     * 根据字典类型code获取字典map key = 字典名称  value = 字典值 (数据库存的是字典名称,类似分类 , 例如:重心币)
     * @param dicTypeCode
     * @return
     */
    public Map<String,String> getDictionaryMapByTypeCode2(String dicTypeCode){
        LambdaQueryWrapper<Dictionary> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dictionary::getIsDeleted,IsDeletedEnum.NO.getFlag());
        lambdaQueryWrapper.eq(Dictionary::getDicTypeCode,dicTypeCode);
        lambdaQueryWrapper.orderByAsc(Dictionary::getDicSort);
        List<Dictionary> list =  dictionaryMapper.selectList(lambdaQueryWrapper);
        return list.stream().collect(Collectors.toMap(Dictionary::getDicName,Dictionary::getDicValue,(v1,v2) -> v2));
    }
    /**
     * 根据字典类型code数组获取字典列表 , 然后在分组返回
     * key = 字典类型code  value = 字典列表 (数据库存的是字典名称,类似分类 , 例如:重心币)
     * @param dto
     * @return
     */
    public Map<String,List<Dictionary>> selectDictionaryByTypeCodeSet(SelectDictionaryByTypeSetDto dto) {
        LambdaQueryWrapper<Dictionary> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dictionary::getIsDeleted,IsDeletedEnum.NO.getFlag());
        lambdaQueryWrapper.in(Dictionary::getDicTypeCode,dto.getDicTypeCodeSet());
        lambdaQueryWrapper.orderByAsc(Dictionary::getDicSort);
        List<Dictionary> list =  dictionaryMapper.selectList(lambdaQueryWrapper);
        return list.stream().collect(Collectors.groupingBy(Dictionary::getDicTypeCode));
    }
}
